package getui

import (
	"bytes"
	"net/http"
	"time"
)

//sendPost makes a http POST request with bodyByte data
func sendPost(url string, token string, bodyByte []byte) ([]byte, error) {
	//创建客户端实例
	client := &http.Client{
		Timeout: 10 * time.Second,
	}

	body := bytes.NewBuffer(bodyByte)

	//创建请求实例
	req, err := http.NewRequest("POST", url, body)
	if err != nil {
		return nil, err
	}

	if len(token) > 0 {
		req.Header.Add("token", token)
	}

	req.Header.Add("Content-Type", "application/json;charset=utf-8")

	//发起请求
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	buf := bytes.Buffer{}
	_, err = buf.ReadFrom(resp.Body)
	if err != nil {
		return nil, err
	}

	return buf.Bytes(), nil
}
